local ret = {}

local skel = fk.CreateSkill {
  name = "rmt__tufub",
  tags = {Skill.Compulsory}
}

Fk:loadTranslationTable{
  ["rmt__tufub"] = "徒复",
  [":rmt__tufub"] = "锁定技，当你扣减体力上限至体力后，选择一项：攻击范围+1，或将手牌摸至手牌上限；当你回复体力至上限后，选择一项："..
  "手牌上限+1，或获得攻击范围内所有角色各一张牌。",

  ["rmt__tufub_attackrange"] = "攻击范围+1",
  ["rmt__tufub_draw"] = "将手牌摸至手牌上限",
  ["#rmt__tufub_choose"] = "选择",
  ["#rmt__tufub-prey-or-maxcard"] = "徒复：选择手牌上限+1，或获得攻击范围内所有角色各一张牌",
  ["@@rmt__tufub_can_prey"] = "可偷牌",
  ["@@rmt__tufub_nude"] = "我没牌",
  ["rmt__tufub_maxcards"] = "手牌上限+1",
  ["rmt__tufub_prey"] = "获得攻击范围内角色各一张牌",
  ["#rmt__tufub-prey"] = "徒复：获得 %dest 一张牌",

  ["$rmt__tufub1"] = "外假秦声，内规兴复，端拱可定东夏。",
  ["$rmt__tufub2"] = "天启嘉会、灵命暂迁，此鸿渐之始，龙变之初。",
}


skel:addEffect(fk.HpRecover, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return not player:isWounded()
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = "rmt__tufub_maxcards"
    local targets = table.filter(room:getOtherPlayers(player), function (p)
      return player:inMyAttackRange(p) and not p:isNude()
    end)
    if #targets > 0 then
      local success, dat = room:askToUseActiveSkill(player, {
        skill_name = "#rmt__tufub_choose", prompt = "#rmt__tufub-prey-or-maxcard", cancelable = false
      })
      if success and dat then
        choice = dat.interaction
      end
    end
    if choice == "rmt__tufub_maxcards" then
      room:addPlayerMark(player, MarkEnum.AddMaxCards, 1)
    else
      -- 插结摸牌也得被偷
      targets = table.filter(room:getOtherPlayers(player), function (p)
        return player:inMyAttackRange(p)
      end)
      room:doIndicate(player, targets)
      for _, p in ipairs(targets) do
        if player.dead then return end
        if not p.dead and not p:isNude() then
          local card = room:askToChooseCard(player, {
            target = p, skill_name = skel.name, flag = "he", prompt = "#rmt__tufub-prey::"..p.id
          })
          room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, skel.name, nil, false, player)
        end
      end
    end
  end,
})

skel:addEffect(fk.MaxHpChanged, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return not player:isWounded() and (data.extra_data or {}).rmt__tufub_MaxHpChangeCheck
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {"rmt__tufub_attackrange"}
    if player:getHandcardNum() < player:getMaxCards() then
      table.insert(choices, "rmt__tufub_draw")
    end
    local choice = room:askToChoice(player, {choices = choices, skill_name = skel.name})
    if choice == "rmt__tufub_attackrange" then
      room:addPlayerMark(player, skel.name, 1)
    else
      player:drawCards(player:getMaxCards() - player:getHandcardNum(), skel.name)
    end
  end,
})

skel:addEffect(fk.BeforeMaxHpChanged, {
  anim_type = "control",
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name, true) and data.num < 0 and player:isWounded()
  end,
  on_refresh = function (self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.rmt__tufub_MaxHpChangeCheck = true
  end,
})

skel:addEffect("atkrange", {
  correct_func = function (self, from, to)
    return from:getMark("rmt__tufub")
  end,
})


local skel2 = fk.CreateSkill {
  name = "#rmt__tufub_choose",
}

skel2:addEffect("active", {
  interaction = function()
    return UI.ComboBox { choices = {"rmt__tufub_maxcards", "rmt__tufub_prey"} }
  end,
  card_num = 0,
  target_num = 0,
  target_filter = Util.FalseFunc,
  target_tip = function (self, player, to_select, selected, selected_cards, card, selectable, extra_data)
    if player:inMyAttackRange(to_select) then
      if to_select:isNude() then
        return { {content = "@@rmt__tufub_nude", type = "warning"} }
      else
        return "@@rmt__tufub_can_prey"
      end
    end
  end,
})


table.insert(ret, skel)
table.insert(ret, skel2)

return ret
